import homeApi from "@/static/js/enterpriseServer/index.js"
import choseRegion from '@/components/chose-region/chose-region.vue'
import wfAddress from "@/components/WF-address/WF-address.vue";
export default {
	components: {
		choseRegion,wfAddress
	},
	data() {
		return {
			//需求列表数据
			pageShow: false,
			demandList: [],

			//省市区
			choseRegion: [],
			cityCode: '',

			//服务类目
			categoryShow: false,
			tabCur: 0,
			categoryList: [],
			cateFirstId: '',
			cateSecondId: '',
			cateSecondList: [],
			cateThirdId: '',
			cateThirdName: '擅长服务',

			//需求状态
			sortValue: 0,
			sortData: [
				{
					id: '',
					name: '全部'
				},
				{
					id: '0',
					name: '可参与'
				},
				{
					id: '1',
					name: '已中标'
				}
			],
			sortKey: 0,

			//发布时间
			dateValue: 0,
			dateData: [
				{
					id: '0',
					name: '全部'
				},
				{
					id: '1',
					name: '今日'
				},
				{
					id: '3',
					name: '最近三天'
				},
				{
					id: '7',
					name: '最近一周'
				},
				{
					id: '30',
					name: '最近一个月'
				}
			],
			dateKey: 0,

			//分页参数
			page: 1,
			pageSize: 10,
			isRefreshing: false,
			listStatus: 'nomore',
			choseRegion1: null,
      showCityList: false,
		}
	},
	onLoad() {
		this.getCategoryData()
		this.getData()
	},
	methods: {
		//获取需求列表数据
		async getData() {
			this.listStatus = 'loading'
			uni.showLoading({
				title: '加载中'
			})
			let obj;
			if (!this.sortData[this.sortKey].id) {
				obj = {
					page: this.page,
					pagesize: this.pageSize,
					one_cate_id: this.cateFirstId,
					two_cate_id: this.cateSecondId,
					third_cate_id: this.cateThirdId,
					time_type: this.dateData[this.dateKey].id,
					city_code: this.cityCode
				}
			} else {
				obj = {
					page: this.page,
					pagesize: this.pageSize,
					one_cate_id: this.cateFirstId,
					two_cate_id: this.cateSecondId,
					third_cate_id: this.cateThirdId,
					status: this.sortData[this.sortKey].id,
					time_type: this.dateData[this.dateKey].id,
					city_code: this.cityCode
				}
			}

			let res = await homeApi.getDemandList(obj)
			if (res.code === 1) {
				let arr = []
				if (this.page == 1) {
					this.demandList = []
				}
				let newList = res.data.data
				if (newList.length >= this.pageSize) {
					this.listStatus = 'more'
				} else {
					this.listStatus = 'nomore'
				}
				let arr1 = arr.concat(newList)
				arr1.forEach(val => {
					let obj = {
						id: val.id,
						name: val.name,
						describe: val.describe,
						money: val.money,
						city_name: val.city_name,
						status: val.status,
						join_number: val.join_number
					}
					this.demandList.push(obj)
				})
				this.pageShow = true
				this.isRefreshing = false
				uni.hideLoading()
			}
		},

		//省市切换
		regionName(res) {
			this.page = 1
			this.choseRegion = res
			this.cityCode = res[1].id
			this.getData()
		},
		regionValue(res) {
			console.log(res)
		},

		//需求状态
		changeSort(e) {
			this.page = 1
			this.sortKey = e.detail.value
			this.getData()
		},

		//需求发布时间
		changeDate(e) {
			this.page = 1
			this.dateKey = e.detail.value
			this.getData()
		},

		//服务类目弹框
		showCategory() {
			this.cateFirstId = this.categoryList[0].id
			this.getSecondCate()
			this.categoryShow = true
		},

		//获取服务类目
		async getCategoryData() {
			let res = await homeApi.getServiceCate()
			if (res.code === 1) {
				this.categoryList = res.data
			} else {
				this.$tools.msg(res.msg)
			}
		},

		//服务分类选择全部
		choseAll() {
			this.cateThirdName = ''
			this.cateFirstId = ''
			this.cateSecondId = ''
			this.cateThirdId = ''
			this.getData()
			this.categoryShow = false
		},

		//服务类目切换
		tabSelect(item, index) {
			this.tabCur = index;
			this.cateFirstId = item.id
			this.getSecondCate()
		},

		//获取二类三类服务类目
		async getSecondCate() {
			let obj = {
				c_id: this.cateFirstId
			}
			let res = await homeApi.getServiceCateChild(obj)
			if (res.code === 1) {
				this.cateSecondList = res.data
			} else {
				this.$tools.msg(res.msg)
			}
		},
		//三级类目点击事件
		thirdClick(val) {
			this.page = 1
			this.cateThirdName = val.name
			this.cateThirdId = val.id
			this.cateSecondId = val.s_id
			this.cateFirstId = val.c_id
			this.categoryShow = false
			this.getData()
		},

		//跳转需求详情
		goDetail(item) {
			uni.navigateTo({
				url: '/enterpriseServer/demandDetails/index?id=' + item.id
			})
		},

		//跳转发布需求
		goPostDemand() {
			uni.navigateTo({
				url: '/enterpriseServer/publishDemand/index'
			})
		},

		//下拉刷新
		refresherrefresh() {
			this.isRefreshing = true;
			this.page = 1
			this.getData()
		},

		//上拉加载
		loadMore() {
			if (this.listStatus == 'more') {
				this.page += 1
				this.getData()
			}
		},



		showCityListFn() {
      this.showCityList = true;
    },

    closeCityList() {
      this.showCityList = false;
      this.choseRegion1 = null;
      this.cityCode = "";
      this.getData();
    },

    getCityInfo(data) {
      this.choseRegion1 = data;
      this.cityCode = data.id;
      this.showCityList = false;
      this.getData();
    },


    cateName(e){
      // this.tabCur = index;
      this.cateSecondId = e[1].id;
      this.cateThirdName = e[1].name;
      this.page = 1;
      this.getData();
		},


		cancels() {
      this.cateThirdName = "擅长服务";
      this.cateSecondId = "";
      this.getData();
    },
	}
}